package com.everflourish.yeah100.http;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.everflourish.yeah100.utils.CommonUtil;
import com.everflourish.yeah100.utils.constant.RoleEnum;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 注册部分的接口封装
 *
 * @author pf
 * @date 2015-1-14
 */
public class LoginAndRegisterRequest extends BaseRequest {

    private LoginAndRegisterRequest() {
    }

    private static LoginAndRegisterRequest mRequest;

    public static LoginAndRegisterRequest getInstance() {
        if (mRequest == null) {
            mRequest = new LoginAndRegisterRequest();
        }
        return mRequest;
    }

    /**
     * 班号、学号验证登录
     *
     * @param classNo       班号
     * @param studentNo     学号
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest studentVerification(String classNo,
                                                 String studentNo, Listener<JSONObject> listener,
                                                 ErrorListener errorListener) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("classId", classNo);
        map.put("studentNo", studentNo);
        JSONObject jsonObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_STUDENT_VERIFICATION, jsonObject, listener, errorListener);
        return request;
    }

    /**
     * 用户登录
     *
     * @param userName      用户名
     * @param password      密码
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest userLogin(String userName, String password,
                                       Listener<JSONObject> listener, ErrorListener errorListener) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("j_username", userName);
        map.put("j_password", password);
        // map.put(" j_password", CommonUtils.md5(password));
        JSONObject jsonObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_SECURITY_LOGIN, jsonObject, listener, errorListener);
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS10,
                DEFAULT_MAX_RETRIES1, DEFAULT_BACKOFF_MULT));
        return request;
    }

    /**
     * 关联登录
     *
     * @param userName      用户名
     * @param password      密码
     * @param classNo       班号
     * @param studentNo     学号
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest userLogin(String userName, String password,
                                       String classNo, String studentNo, Listener<JSONObject> listener,
                                       ErrorListener errorListener) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("j_username", userName);
        map.put("j_password", password);
        map.put("classId", classNo);
        map.put("studentNo", studentNo);
        // map.put(" j_password", CommonUtils.md5(password));
        JSONObject jsonObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_SECURITY_LOGIN, jsonObject, listener, errorListener);
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS,
                DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT));
        return request;
    }

    /**
     * 用户注册申请
     *
     * @param userName      用户名
     * @param password      密码
     * @param email         邮箱
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest userEmailRegister(String userName,
                                               String password, String email, Listener<JSONObject> listener,
                                               ErrorListener errorListener) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("username", userName);
        map.put("password", CommonUtil.string2MD5(password));
        map.put("email", email);
        JSONObject jsonObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_REGISTER_EMAIL, jsonObject, listener, errorListener) {
            private int count = 1;

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                System.out.println("个数：" + count++);
                return new HashMap<String, String>();
            }
        };
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS,
                DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT));
        return request;
    }

    /**
     * 用户手机注册
     *
     * @param username
     * @param password
     * @param phoneZone
     * @param phoneNo
     * @param verificationCode
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest userPhoneRegister(String username,
                                               String password, String phoneZone, String phoneNo,
                                               String verificationCode, Listener<JSONObject> listener,
                                               ErrorListener errorListener) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("username", username);
        map.put("password", CommonUtil.string2MD5(password));
        map.put("phoneZone", phoneZone);
        map.put("phoneNo", phoneNo);
        map.put("verificationCode", verificationCode);
        JSONObject jsonObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_REGISTER_PHONE, jsonObject, listener, errorListener);
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS10,
                DEFAULT_MAX_RETRIES1, DEFAULT_BACKOFF_MULT));
        return request;
    }


    /**
     * 获取手机验证码
     *
     * @param phoneZone     区号如： 86
     * @param phoneNo       手机号码
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest getVerificationCodeRequest(String phoneZone,
                                                        String phoneNo, Listener<JSONObject> listener,
                                                        ErrorListener errorListener) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("phoneZone", phoneZone);
        map.put("phoneNo", phoneNo);
        JSONObject jsonObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_PHONE_VERIFICATION_CODE, jsonObject, listener,
                errorListener);
        return request;
    }

    /**
     * 找回密码
     *
     * @param email         邮箱
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest findPassword(String email,
                                          Listener<JSONObject> listener, ErrorListener errorListener) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("email", email);
        JSONObject jsonObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_LOGIN_EMAIL, jsonObject, listener, errorListener);
        return request;
    }

    /**
     * 获取手机验证码
     *
     * @param phoneZone
     * @param phoneNo
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest getPasswordPhoneVerificationCodeRequest(
            String phoneZone, String phoneNo, Listener<JSONObject> listener,
            ErrorListener errorListener) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("phoneZone", phoneZone);
        map.put("phoneNo", phoneNo);
        JSONObject jsonObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_PASSWORD_PHONE_VERIFICATION_CODE, jsonObject, listener,
                errorListener);
//        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS_2,
//                DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT));
        return request;
    }

    /**
     * 手机验证修改密码
     *
     * @param phoneNo
     * @param phoneZone
     * @param password
     * @param verificationCode
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest changePasswordByPhoneRequest(String phoneNo,
                                                          String phoneZone, String password, String verificationCode,
                                                          Listener<JSONObject> listener, ErrorListener errorListener) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("phoneNo", phoneNo);
        map.put("phoneZone", phoneZone);
        map.put("password", CommonUtil.string2MD5(password));
        map.put("verificationCode", verificationCode);
        JSONObject jsonObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_PASSWORD_CHANGE_PHONE_VERIFICATION_CODE, jsonObject,
                listener, errorListener);
        return request;
    }

    /**
     * 角色切换
     *
     * @param roleEnum
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest roleUpdateRequest(RoleEnum roleEnum,
                                               Listener<JSONObject> listener, ErrorListener errorListener) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("role", roleEnum.name);
        JSONObject jsonObject = new JSONObject(map);

        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_USER_ROLE_UPDATE, jsonObject, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }
}
